require("../../assets/css/index.less");
require("./login.less");
require("../../assets/fonts_two/iconfont");
const axios = require("axios");
require('../../utils/https');
axios.defaults.headers.common['Authorization'] = "Bearer " + localStorage.getItem("token");

function $(a) {
    return document.querySelector(a);
}

function $$(a) {
    return document.querySelector(a);
}

$(".head").addEventListener("click", () => {
    location.href = "register.html";
})

let a = /^[0-9a-zA-Z]{6,12}$/;
$("#text").addEventListener("blur", () => {
    console.log(1);
    if (!a.test($("#text").value)) {
        $("#text").style.color = "red";
        $("#text").value = "请输入6-12位密码";
    } else {
        $("#text").value = $("#text").value;
        $("#text").style.color = "#fff";
    }
});

$("#text").addEventListener("click", () => {
    if ($("#text").style.color === "red") {
        $("#text").value = "";
    }
    $("#text").style.color = "#fff";
})

$("#pwd").addEventListener("blur", () => {
    if (!a.test($("#pwd").value)) {
        $("#pwd").style.color = "red";
        $("#pwd").type = "text";
        $("#pwd").value = "请输入6-12位密码";
    } else {
        $("#pwd").value = $("#pwd").value;
        $("#pwd").type = "password";
        $("#pwd").style.color = "#fff";
    }
});

$("#pwd").addEventListener("click", () => {
    if ($("#pwd").style.color === "red") {
        $("#pwd").value = "";
        $("#pwd").style.color = "#fff";
        $("#pwd").type = "password";
    }
})

$("#btn").addEventListener("click", function() {
    // console.log($("#text").value);
    const fn = async function(account, password) {
        const res = await axios.post("/api/user/login", {
            "account": account,
            "password": password,
        });
        console.log(res.data.data.token);
        if (res.data.errno === 0) {
            $(".box").classList.remove("none");
            localStorage.setItem("token", JSON.stringify(res.data.data.token));
            setInterval(() => {
                location.href = "/home.html";
            }, 3000);
        } else {
            $(".box_two").classList.remove("none")
            setInterval(() => {
                $(".box_two").classList.add("none")
            }, 3000)
        }
    }
    fn($("#text").value, $("#pwd").value)
})